﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using DTO;
using System.Data.Sql;

namespace DAO
{
    public class ChiTietThauSachDAO
    {
        DataProvider provider;

        public ChiTietThauSachDAO()
        {
            provider = new DataProvider();
        }

        public int ThemChiTietThauSach(ChiTietThauSachDTO chiTietThauSach)
        {
            string sqlStatement = "INSERT INTO ChiTietThauSach(MaThauSach, MaTaiKhoan, ThoiGianDauThau, DaXoa) VALUES (" 
                + chiTietThauSach.MaThauSach + ", " + chiTietThauSach.MaTaiKhoan + ", '" + chiTietThauSach.ThoiGianDauThau + "', 0)";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int CapNhatChiTietThauSach(ChiTietThauSachDTO chiTietThauSach)
        {
            string sqlStatement = "UPDATE ChiTietThauSach SET MaThauSach=" + chiTietThauSach.MaThauSach + ", MaTaiKhoan=" + chiTietThauSach.MaTaiKhoan
                + ", ThoiGianDauThau='" + chiTietThauSach.ThoiGianDauThau + "' WHERE MaCTThauSach=" + chiTietThauSach.MaCTThauSach;
            
            return provider.ExecuteNonQuery(sqlStatement);
        }
        
        public int XoaChiTietThauSach(ChiTietThauSachDTO chiTietThauSach)
        {
            string sqlStatement = "UPDATE ChiTietThauSach SET DaXoa=1 WHERE MaCTThauSach=" + chiTietThauSach.MaCTThauSach;

            return provider.ExecuteNonQuery(sqlStatement);
        }
        
        public DataTable LayDanhSachCTThauSachTheoMaThauSach(ThauSachDTO thauSach)
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT ts.TenThauSach, ts.NgayThangNamBatDau, ts.ThoiGianBatDau, ts.KhoangThoiGianDienRaDauThau, ts.SoTienCTYRaThau, ts.SoTienCuaNhaCungCapTraThau, ts.LanDauThauThu, ts.MaTaiKhoan, ts.DaDuyet "
                    + "FROM ThauSach ts, ChiTietThauSach ctts "
                    + "WHERE ts.MaThauSach=ctts.MaThauSach AND ctts.MaThauSach=" + thauSach.MaThauSach + " ORDER BY ctts.ThoiGianDauThau DESC";
                dt = provider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return dt;
        }
    }
}
